$(function () {
    $('.order-all').click(function () {
        $('.order-type').addClass('show-order');
    });
    $('.close').click(function () {
        $('.order-type').removeClass('show-order');
    });

    $('#pj-close').click(function () {
        $('#order-comment').hide();
    });

    $('.pinjia').click(function () {
        $('.pop-box').css('display', 'block');
        var val = $(this).data('id');
        var order_id = $(this).data('order_id');
        $('.rstid').val(val);
        $('.order_id').val(order_id);
    });

    $('.star').click(function () {
        $(this).removeClass('grey-star');
        $(this).prevAll().removeClass('grey-star');
        $(this).nextAll().addClass('grey-star');
    });

    //评价订单
    $(document).on('click', '.pj-btn', function () {
        var score1 = $('.star1').find(".grey-star").length;
        var score2 = $('.star2').find(".grey-star").length;
        var score3 = $('.star3').find(".grey-star").length;
        var id = $(".rstid").val();
        var comment = $('#comment').val();
        var order_id = $(".order_id").val();
        var res_id = $('#res_id').val();
        if (score1 == 5) {
            $.alert('error', '请评价口味');
            return false;
        }

        if (score2 == 5) {
            $.alert('error', '请评价质量');
            return false;
        }
        if (score3 == 5) {
            $.alert('error', '请评价配送');
            return false;
        }

        var data = {};
        var json = {};
        data.score = 5 - score1;
        data.score_send = 5 - score3;
        data.score_zl = 5 - score2;
        data.comment = comment;
        data.id = id;
        data.order_id = order_id;
        json.data = data;
        json.url = '/base/restaurant/comment?res_id=' + res_id;
        json.method = 'post';
        json.type = 'json';
        $.getAjax(json, true, reload, '');
        $('.pop-box').css('display', 'none');
    });


    //取消订单
    $(document).on('click', '.reset', function () {
        var order_id = $(this).data('order_id');
        var res_id = $('#res_id').val();
        $.confirm("确定取消该订单吗？", drop);
        function drop() {
            var data = {};
            var json = {};
            data.order_id = order_id;
            json.data = data;
            json.url = '/base/restaurant/resetOrder?res_id=' + res_id;
            json.method = 'post';
            json.type = 'json';
            $.getAjax(json, true, reload, '');
        }
    });

    //取消订单 退款
    $(document).on('click', '.active', function () {
        var order_id = $('#order_id').val();
        var res_id = $('#res_id').val();
        var reason = $('#reason').val();
        $.confirm("确定取消该订单,申请退款吗？", drop);
        function drop() {
            var data = {};
            var json = {};
            data.order_id = order_id;
            data.reason = reason;
            json.data = data;
            json.url = '/base/restaurant/applyOrder?res_id=' + res_id;
            json.method = 'post';
            json.type = 'json';
            $.getAjax(json, true, reload, '');
        }
    });

    //确认收货
    $(document).on('click', '.sure-btn', function () {
        var order_id = $(this).data('order_id');
        var res_id = $('#res_id').val();
        $.confirm("确认收货吗？", drop);
        function drop() {
            var data = {};
            var json = {};
            data.order_id = order_id;
            json.data = data;
            json.url = '/base/restaurant/sureOrder?res_id=' + res_id;
            json.method = 'post';
            json.type = 'json';
            $.getAjax(json, true, reload, '');
        }
    });

    //提交订单
    $(document).on('click', '.pay-btn', function () {
        var send_time = $('#send_time').val();
        var address_id = $('.address_id').val();
        var username = $('.username').val();
        var mobile = $('.mobile').val();
        var address = $('.address').val();
        var restaurant_id = $('.restaurant_id').val();
        var memo = $('#memo').val();
        var order_id = $(this).data('order_id');
        var res_id = $('#res_id').val();
        if ($(".weipay_check").length <= 0) {
            $.alert("error", "请选择支付方式");
            return false;
        }

        var pay_type = $(".weipay_check").data("type");
        var stored_value_id = $("#stored_value_id").val();
        if (pay_type == 3) {
            var no_bind = $("#no_bind").val();
            if (no_bind == 1) {
                var parames = $("#parames").val();
                $.alert("error", "未绑定虚拟e卡");
                window.sessionStorage.setItem("local_url", "/base/StoredValue/bound/stored_value_id/" + stored_value_id + "/is_restaurant/1/parames/" + parames);
                setTimeout(redirect, 2000);
                return false;
            }
        }

        if (!address) {
            $.alert('error', '请选择配送地址');
            return false;
        }

        if (!address_id) {
            $.alert('error', '请选择配送地址');
            return false;
        }

        if (res_id != 16) {
            if (!send_time) {
                $.alert('error', '该餐厅已停止配送，请选择其他餐厅');
                return false;
            }
        }
        var info = {};
        var json = {};
        info.order_id = order_id;
        info.send_time = send_time;
        info.address_id = address_id;
        info.username = username;
        info.mobile = mobile;
        info.memo = memo;
        info.address = address;
        info.restaurant_id = restaurant_id;
        info.pay_type = pay_type;
        info.stored_value_id = stored_value_id;
        if (pay_type == 3) {
            $.confirm("是否确认使用虚拟e卡余额付款？", storedPay);
        } else {
            storedPay();
        }
        function storedPay() {
            $.ajax({
                'url': "/base/restaurant/orderPay?res_id=" + res_id,
                'data': info,
                'dataType': 'json',
                'type': 'POST',
                'async': true,
                success: function (data) {
                    if (data.status == 'S') {
                        var wxpay = $.parseJSON(data.wxpay);
                        pay(wxpay);
                    } else if (data.status == 'BS') {
                        $.alert('success', '付款成功');
                        window.sessionStorage.setItem("local_url", "/base/restaurant/myOrder/res_id/" + res_id);
                        setTimeout(redirect, 2000);
                    } else {
                        $.alert('error', data.message);
                        return false;
                    }
                }
            });
        }

    });


    //微支付
    function pay(wxpay) {
        var res_id = $('#res_id').val();
        if (typeof WeixinJSBridge == "undefined") {
            if (document.addEventListener) {
                document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
            } else if (document.attachEvent) {
                document.attachEvent('WeixinJSBridgeReady', jsApiCall);
                document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
            }
        } else {
            WeixinJSBridge.invoke(
                'getBrandWCPayRequest',
                wxpay,
                function (res) {
                    WeixinJSBridge.log(res.err_msg);
                    if (res.err_msg == "get_brand_wcpay_request:ok") {
                        paySuccess();
                    } else if (res.err_msg == "get_brand_wcpay_request:cancel") {
                        $.alert('error', '付款已取消');
                        setTimeout(location.href = "/base/restaurant/myorder/res_id/" + res_id, 2000);
                    } else {
                        $.alert('error', '调用微支付失败');
                        return false;
                    }
                }
            );
        }
    }

    function paySuccess() {
        var res_id = $('#res_id').val();
        location.href = "/base/restaurant/myOrder/res_id/" + res_id;
    }

    function redirect() {
        window.location.href = window.sessionStorage.getItem("local_url");
        window.sessionStorage.setItem("local_url", null);
    }

    //页面刷新
    function reload() {
        setTimeout('location.reload()', 2000);
    }
});